<template>
  <div>
    <div class="search_box">
      <el-form
        :inline="true"
        ref="ruleFormRef"
        :model="formData"
        class="form_box"
        @submit.prevent
      >
        <el-form-item label="模板名称">
          <el-input
            v-model="formData.name"
            placeholder="模板名称"
            @clear="getList(1)"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="getList(1)"
            v-auth="'api:agent:template:search'"
            >查询</el-button
          >
          <span
            style="margin: 0 10px; color: #666"
            v-auth-all="['api:agent:template:add', 'api:agent:template:search']"
            >|</span
          >
          <el-button
            type="primary"
            @click="add"
            v-auth="'api:agent:template:add'"
          >
            <el-icon style="margin-right: 5px">
              <Plus />
            </el-icon>
            新 增
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- table列表 -->
    <div class="table_box">
      <p class="tips">
        <el-alert
          title="将常用的充值模板添加至模板库，可以在配置模板时更方便地引用"
          type="warning"
          :closable="false"
        />
      </p>
      <el-table
        :data="tableData"
        style="width: 100%"
        row-key="id"
        :expand-row-keys="expands"
        v-loading="loading"
        :height="tableHeight"
        ref="tableRef"
      >
        <el-table-column type="expand" width="80">
          <template #default="scope">
            <div m="4" class="open_box">
              <defaultTemplate
                v-if="scope.row.items"
                :relationData="scope.row.items"
                :isHover="false"
              />
            </div>
          </template>
        </el-table-column>

        <el-table-column label="名称" prop="name" />
        <el-table-column label="充值金额" prop="desc" />
        <el-table-column label="操作" fixed="right" width="160">
          <template #default="props">
            <span
              class="operationClick"
              type="primary"
              @click="handleClick(props.row, '编辑')"
              v-auth="'api:agent:template:update'"
            >
              编辑
            </span>
            <span
              class="operationClick"
              type="primary"
              @click="handleClick(props.row, '删除')"
              v-auth="'api:agent:template:delete'"
            >
              删除
            </span>
            <!-- @click="copyLink(JSON.stringify(props.row?.items))" -->
            <span
              class="operationClick"
              type="primary"
              @click="handleClick(props.row, '复制')"
            >
              复制
            </span>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <customPagination
        class="pageBox"
        :pageObj="pageObj"
        @sizechange="sizechange"
        @currentChange="currentChange"
      />
    </div>

    <!-- 添加/模板 -->
    <addTemplateDialog ref="addTempalteRef" @success="successGetList" />
  </div>
</template>

<script setup lang="ts">
import { Plus } from "@element-plus/icons-vue";
import defaultTemplate from "@/components/DefaultRechargeTemlate/defaultTemlate.vue";
import addTemplateDialog from "./components/addTemplateDialog.vue";
import {
  productTmplLibrary,
  removeTmplLibrary,
} from "@/api/globalManage/rechargeTemplate/index";
import { copyLink } from "@/utils/tools";
import { ref, reactive } from "vue";

const ruleFormRef = ref();
const loading = ref<boolean>(false);

const form = {
  pageNo: 1,
  pageSize: 10,
  name: "",
};
const formData = reactive(Object.assign({}, form));
const pageObj = reactive({
  pageNo: 1,
  pageSize: 10,
  total: 0,
});
const tableData = ref([]);
const tableHeight = ref();
const tableRef = ref();

// 查询列表
const getList = (val = 0) => {
  // if (!val) {
  //   formData.pageNo = pageObj.pageNo;
  //   formData.pageSize = pageObj.pageSize;
  // } else {
  //   formData.pageNo = pageObj.pageNo = val === 1 ? val : 1;
  //   formData.pageSize = pageObj.pageSize = val === 1 ? 10 : pageObj.pageSize;
  // }
  formData.pageNo = pageObj.pageNo;
  formData.pageSize = pageObj.pageSize;

  loading.value = true;
  productTmplLibrary(formData)
    .then((res: Record<string, any>) => {
      tableData.value = res.data;
      pageObj.total = Number(res?.total);
      loading.value = false;
    })
    .finally(() => {
      loading.value = false;
    });
};

const addTempalteRef = ref();
const add = () => {
  addTempalteRef.value.showDialog("", "新增");
};

// 修改成功刷新列表
const expands = ref<any>([]);
const successGetList = () => {
  expands.value = []; // 所所有展开行关闭
  getList(1);
};

// 操作
const handleClick = (val: Record<string, any>, text: string) => {
  if (text == "编辑") {
    addTempalteRef.value.showDialog(val, "编辑");
  } else if (text == "删除") {
    ElMessageBox.confirm("确认删除这条数据吗?", "提醒", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        removeTmplLibrary(val.id).then((res: Record<string, any>) => {
          ElMessage.success("删除成功");
          getList(1);
        });
      })
      .catch(() => {});
  } else if (text == "复制") {
    localStorage.setItem("copyTemp", JSON.stringify(val?.items));
    ElMessage.success("复制成功");
  }
};

const currentChange = (val: number) => {
  pageObj.pageNo = val;
  getList();
};

const sizechange = (val: number) => {
  pageObj.pageSize = val;
  getList(2);
};

onMounted(() => {
  getList();
  // 设置表格初始高度为innerHeight-offsetTop-表格底部与浏览器底部距离：66 + 顶部固定高度：50
  tableHeight.value = window.innerHeight - tableRef.value.$el.offsetTop - 118;
  // 监听浏览器高度变化
  window.onresize = () => {
    tableHeight.value = window.innerHeight - tableRef.value.$el.offsetTop - 118;
  };
});
</script>

<style lang="scss" scoped>
.table_box {
  // height: calc(100vh - 150px);
  padding: 10px;
  margin-top: 10px;
  // overflow-y: scroll;
  background-color: #fff;
  border-radius: 4px;

  :deep(th.el-table__cell) {
    background-color: #efefef;
  }

  .tips {
    padding: 10px 0 0;
    margin: unset;
    margin-bottom: 12px;
    font-size: 16px;
    color: #333;
  }
}

.p {
  margin: 3px 0;
}

.open_box {
  padding-left: 80px;
}
</style>
